# -*- coding: utf-8 -*-
import wsgiref.handlers
import xmw
from google.appengine.ext import webapp
from google.appengine.ext import db

from xmw.db import ds
from xmw.db import counter
from xmw.db import user_mgr
import logging

from google.appengine.api import mail
class MainPage(xmw.RequestHandler):
    def get(self):        
        #template_values={"step":1}                
        self.render_template(None, "../html/register.html")
        return
    
    def post(self):
        (form_email,e) = self.get_key_from_POST('form_email')
        #print form_email
        (form_password,e) = self.get_key_from_POST('form_password')
        (form_password2,e) = self.get_key_from_POST('form_password2')
        #print form_password
        (form_name,e) = self.get_key_from_POST('form_name')
        #print form_name
        
        form_email  =form_email.strip().decode('utf-8')
        form_password  =form_password.strip().decode('utf-8')
        form_password2  =form_password2.strip().decode('utf-8')
        """
        u_q=ds.User.all()
        u_q.filter("email=", form_email).filter("name =", form_name)
        user =u_q.get()
        """
        logging.log( logging.INFO, "register email %s,and password %s,name=%s"% (form_email,form_password,form_name))
        user=user_mgr.get_user_by_mail(form_email)
        if form_password!=form_password2:
            template_values={"form_email":form_email,
                             "form_password":form_password,
                             "form_password2":form_password2,
                             "form_name":form_name,
                             "pass_not_match":True} 
            self.render_template(template_values, "../html/register.html")
        if user is  None:
            u=ds.User(email=form_email,password=form_password,name=form_name)
            uc=counter.UserCounter()
            u.user_id=uc.inc_and_get()
            #u.put()
            #use save to get china's localtime
            u.save()
            """
            template_values={"email":form_email} 
            self.render_template(template_values, "../html/register_last_step.html")
            """
            template_values={"from_register":True} 
            self.render_template(template_values, "../html/login.html")
            message = mail.EmailMessage(sender="西门外<ximenwai.liu@gmail.com>",
                            subject=u"您已成功注册西门外")

            message.to = form_email
            message.body = u"""
%s 你好!:
        您已经成功在西门外(ximenwai.com)注册用户。
        用户名:%s
       注册email: %s
      
  
                            """%(form_name,form_name,form_email)

            message.send()
        else:
            
            template_values={"form_email":form_email,
                             "form_password":form_password,
                             "form_name":form_name,
                             "is_registered":True} 
            self.render_template(template_values, "../html/register.html")
            
            
        
        return
        
        #self.response.out.write(self.request.POST)
        
def main():
  application = webapp.WSGIApplication(
                                       [('/register', MainPage)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()
